package com.sdry.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.project.entity.ProjectEntity;
import com.sdry.project.entity.ProjectExpertEntity;
import com.sdry.project.mapper.ProjectExpertMapper;
import com.sdry.project.service.ProjectExpertService;
import com.sdry.project.service.ProjectService;
import com.sdry.untils.StringUtil;
import com.sdry.untils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description 评审项目汇总
 * @Author chenBin
 * @Date 2020-08-07 11:13
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/projectSummary")
public class ProjectSummaryController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectExpertService projectExpertService;

    @Autowired
    private ProjectExpertMapper projectExpertMapper;

    /*@RequestMapping(value = "/show")
    public String show() {
        return "project/projectSummary/summary";
    }*/

    @RequestMapping(value = "/show")
    public String show() {
        return "project/projectSummary/summary2021";
    }

    /**
     * 普通用户
     * @return
     */
    /*@RequestMapping(value = "/show2")
    public String show2() {
        return "project/projectSummary/summary2";
    }*/

    /**
     * 普通用户
     * @return
     */
    @RequestMapping(value = "/showCharacterPrize")
    public String showCharacterPrize(Model model) {
        model.addAttribute("idea","人物奖");
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        model.addAttribute("userId",userId);
        return "project/projectSummary/summary_admin_2021_4";
    }

    /**
     * 普通用户
     * @return
     */
    @RequestMapping(value = "/showFirstPrize")
    public String showFirstPrize(Model model) {
        model.addAttribute("idea","一等奖");
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        model.addAttribute("userId",userId);
        return "project/projectSummary/summary_admin_2021_1";
    }

    /**
     * 普通用户
     * @return
     */
    @RequestMapping(value = "/showSecondPrize")
    public String showSecondPrize(Model model) {
        model.addAttribute("idea","二等奖");
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        model.addAttribute("userId",userId);
        return "project/projectSummary/summary_admin_2021_2";
    }


    /**
     * 普通用户
     * @return
     */
    @RequestMapping(value = "/showSanPrize")
    public String showSanPrize(Model model) {
        model.addAttribute("idea","三等奖");
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        model.addAttribute("userId",userId);
        return "project/projectSummary/summary_admin_2021_3";
    }

    /**
     * 普通用户
     * @return
     */
    @RequestMapping(value = "/show2")
    public String show2() {
        return "summary_admin_2021_1";
    }


    /**
     * 修改项目判定等级
     * @param projectId 项目ID
     * @param awardsAdmin 管理员修改等级
     * @return boolean
     */
    @RequestMapping(value = "update")
    public boolean update(Long projectId,String awardsAdmin) {
        try {
            return projectService.updateById(projectId,awardsAdmin);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 评定结果
     * @param map 参数
     * @return getDataTable
     */
    @RequestMapping(value = "queryPage")
    @ResponseBody
    public TableDataInfo queryPage(@RequestParam HashMap<String,Object> map,String idea) {
        QueryWrapper<ProjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(ProjectEntity::getPname,map.get("projectName"));

        List<ProjectEntity> list = projectService.listByExpoer2021(map.get("projectName"),null);
        List<ProjectEntity> listNew1  = new ArrayList<>();
        List<ProjectEntity> listNew4  = new ArrayList<>();
        List<ProjectEntity> listNew7  = new ArrayList<>();
        List<ProjectEntity> listNew8  = new ArrayList<>();
        List<ProjectEntity> listNew  = new ArrayList<>();
        for (ProjectEntity projectEntity : list) {

            String idea1 = projectEntity.getIdea();
            String awardsresult = projectEntity.getAwardsresult();

            if ("1".equals(projectEntity.getAwardsresult())) {
                listNew1.add(projectEntity);
            }else if ("4".equals(projectEntity.getAwardsresult())) {

                if("科技进步奖".equals(projectEntity.getType())) {
                    listNew4.add(projectEntity);
                }else {
                    listNew7.add(projectEntity);
                }
            }else if ("7".equals(projectEntity.getAwardsresult())) {
                listNew7.add(projectEntity);
            } else if (StringUtil.isEmpty(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                if ("一等奖".equals(projectEntity.getIdea())) {
                    listNew1.add(projectEntity);
                }else if ("二等奖".equals(projectEntity.getIdea())) {
                    /*if("科技进步奖".equals(projectEntity.getType())) {
                        listNew4.add(projectEntity);
                    }else {
                        listNew7.add(projectEntity);
                    }*/
                    listNew4.add(projectEntity);
                }else if ("三等奖".equals(projectEntity.getIdea())) {
                    listNew7.add(projectEntity);
                }else if ("人物奖".equals(projectEntity.getIdea())) {
                    listNew8.add(projectEntity);
                }
            }else if ("0".equals(projectEntity.getAwardsresult()) && StringUtil.isNotEmpty(projectEntity.getIdea())) {
                if ("一等奖".equals(projectEntity.getIdea())) {
                    listNew1.add(projectEntity);
                }else if ("二等奖".equals(projectEntity.getIdea())) {
                    /*if("科技进步奖".equals(projectEntity.getType())) {
                        listNew4.add(projectEntity);
                    }else {
                        listNew7.add(projectEntity);
                    }*/
                    listNew4.add(projectEntity);
                }else if ("三等奖".equals(projectEntity.getIdea())) {
                    listNew7.add(projectEntity);
                }else if ("人物奖".equals(projectEntity.getIdea())) {
                    listNew8.add(projectEntity);
                }
            }
        }
        if ("一等奖".equals(idea)) {
            listNew.addAll(listNew1);
        }else if ("二等奖".equals(idea)) {
            listNew.addAll(listNew4);
        }else if ("三等奖".equals(idea)) {
            listNew.addAll(listNew7);
        }else if ("人物奖".equals(idea)) {
            listNew.addAll(listNew8);
        }
        for (ProjectEntity projectEntity : listNew) {
            String realNames = projectExpertService.listUserRelName(projectEntity.getId());
            projectEntity.setExpert(realNames);
        }
        return getDataTable(listNew,null);
    }


    public  String getStringScoring(Long projectId) {
        QueryWrapper<ProjectExpertEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(ProjectExpertEntity::getProjectId,projectId);
        List<ProjectExpertEntity> list2 = projectExpertMapper.selectList(queryWrapper1);
        Integer scoring = 0;
        for (ProjectExpertEntity projectExpertEntity : list2) {
            if (StringUtil.isNotEmpty(projectExpertEntity.getScoring())) {
                scoring += Integer.parseInt(projectExpertEntity.getScoring());
            }
        }
        if (list2.size() == 0) return "";
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format((float)scoring / list2.size());
    }


}
